//class TooltipManager
package baidu.ui.managers 
{
	import baidu.ui.core.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;
	import flash.utils.*;
	
	public class TooltipManager extends Object
	{
		public function TooltipManager()
		{
			this.trigger2Tooltip = new flash.utils.Dictionary(true);
			this.timer = new flash.utils.Timer(4000, 1);
			this.delayTimer = new flash.utils.Timer(500, 1);
			super();
			this.container = new flash.display.Sprite();
			this.container.mouseEnabled = false;
			this.container.tabEnabled = false;
			this.container.mouseChildren = false;
			this.textField = new flash.text.TextField();
			this.textField.background = true;
			this.textField.backgroundColor = 16777164;
			this.textField.border = true;
			this.textField.borderColor = 0;
			this.textField.multiline = false;
			this.textField.wordWrap = false;
			this.textField.autoSize = "left";
			var loc1:*=new flash.text.TextFormat("宋体", 12, 0);
			loc1.indent = 2;
			this.textField.setTextFormat(loc1);
			this.textField.defaultTextFormat = loc1;
			this.timer.addEventListener(flash.events.TimerEvent.TIMER, this.handleTimer);
			this.delayTimer.addEventListener(flash.events.TimerEvent.TIMER, this.toolTipDelayHandler);
			return;
		}
		
		public function get root():flash.display.DisplayObjectContainer
		{
			return this._root;
		}
		
		public function set root(arg1:flash.display.DisplayObjectContainer):void
		{
			if (this._root != null)
			{
				trace("Error:root只允许设置一次，之后不允许修改。");
			}
			else 
			{
				this._root = arg1;
				this._root.addChild(this.container);
			}
			return;
		}
		
		public function get hideDelay():Number
		{
			return this.timer.delay;
		}
		
		public function set hideDelay(arg1:Number):void
		{
			this.timer.delay = arg1;
			return;
		}
		
		public function get defaultRender():*
		{
			return this._defaultRender;
		}
		
		public function set defaultRender(arg1:*):void
		{
			this._defaultRender = arg1;
			return;
		}
		
		private function getRenderInstance(arg1:*):*
		{
			var loc1:*=undefined;
			if (arg1 == null)
			{
				if (this.defaultRender == null)
				{
					loc1 = this.textField;
				}
				else 
				{
					loc1 = baidu.ui.managers.InstanceManager.createSingletonInstance(this.defaultRender);
				}
			}
			else 
			{
				loc1 = baidu.ui.managers.InstanceManager.createSingletonInstance(arg1);
			}
			return loc1;
		}
		
		private function showTooltip(arg1:String, arg2:*, arg3:flash.display.InteractiveObject):void
		{
			var loc3:*=NaN;
			var loc4:*=NaN;
			var loc5:*=NaN;
			var loc6:*=NaN;
			var loc7:*=NaN;
			var loc8:*=NaN;
			var loc1:*=this.getRenderInstance(arg2);
			this.container.addChild(loc1);
			loc1.text = arg1;
			baidu.ui.managers.DepthManager.bringToTop(this.container);
			var loc2:*;
			if ((loc2 = this.container.stage) != null)
			{
				if (loc1 as baidu.ui.core.BUI)
				{
					(loc1 as baidu.ui.core.BUI).drawNow();
				}
				loc3 = loc2.mouseX;
				loc4 = loc2.mouseY;
				loc5 = loc1.width;
				loc6 = loc1.height;
				loc7 = loc2.stageWidth;
				loc8 = loc2.stageHeight;
				if (loc3 + loc5 + 2 > loc7)
				{
					loc1.x = this.container.globalToLocal(new flash.geom.Point(loc7, loc8)).x - loc5 - 2;
				}
				else 
				{
					loc1.x = this.container.mouseX;
				}
				if (loc4 + loc6 + 22 > loc8)
				{
					loc1.y = this.container.mouseY - loc6;
				}
				else 
				{
					loc1.y = this.container.mouseY + 22;
				}
			}
			this.timer.reset();
			this.timer.start();
			return;
		}
		
		private function hideTooltip(arg1:*):void
		{
			var loc1:*=this.getRenderInstance(arg1);
			if (this.container.contains(loc1))
			{
				this.container.removeChild(loc1);
			}
			this.timer.reset();
			this.timer.stop();
			return;
		}
		
		public function register(arg1:flash.display.InteractiveObject, arg2:String, arg3:*=null):void
		{
			if (!this._root)
			{
				this.root = arg1.stage;
			}
			if (arg2 == null)
			{
				arg1.removeEventListener(flash.events.MouseEvent.ROLL_OVER, this.handleTriggerOver, false);
				arg1.removeEventListener(flash.events.MouseEvent.ROLL_OUT, this.handleTriggerOut, false);
				arg1.removeEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.handleTriggerDown, false);
				delete this.trigger2Tooltip[arg1];
				if (this.delayTarget && this.delayTarget == arg1)
				{
					this.delayTimer.stop();
					this.delayTarget = null;
				}
			}
			else 
			{
				this.trigger2Tooltip[arg1] = {"text":arg2, "render":arg3};
				arg1.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.handleTriggerOver, false);
				arg1.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.handleTriggerOut, false);
				arg1.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.handleTriggerDown, false);
			}
			return;
		}
		
		public function unregister(arg1:flash.display.InteractiveObject):void
		{
			this.register(arg1, null);
			return;
		}
		
		private function handleTriggerOver(arg1:flash.events.MouseEvent):void
		{
			if (arg1.buttonDown)
			{
				return;
			}
			this.delayTarget = arg1.target as flash.display.InteractiveObject;
			this.delayTimer.reset();
			this.delayTimer.start();
			return;
		}
		
		private function handleTriggerOut(arg1:flash.events.MouseEvent):void
		{
			var loc1:*=arg1.currentTarget as flash.display.InteractiveObject;
			var loc2:*=this.trigger2Tooltip[loc1]["render"];
			if (this.delayTarget)
			{
				this.delayTimer.stop();
				this.delayTarget = null;
			}
			else 
			{
				this.hideTooltip(loc2);
			}
			return;
		}
		
		private function handleTriggerDown(arg1:flash.events.MouseEvent):void
		{
			this.handleTriggerOut(arg1);
			return;
		}
		
		private function handleTimer(arg1:flash.events.TimerEvent):void
		{
			if (this.container.numChildren)
			{
				this.container.removeChildAt(0);
			}
			return;
		}
		
		private function toolTipDelayHandler(arg1:flash.events.TimerEvent):void
		{
			if (!this.delayTarget)
			{
				return;
			}
			var loc1:*=this.delayTarget;
			var loc2:*=this.trigger2Tooltip[loc1]["text"];
			var loc3:*=this.trigger2Tooltip[loc1]["render"];
			this.showTooltip(loc2, loc3, loc1);
			this.delayTimer.stop();
			this.delayTarget = null;
			return;
		}
		
		public static function getInstance():baidu.ui.managers.TooltipManager
		{
			return baidu.ui.managers.InstanceManager.createSingletonInstance(TooltipManager);
		}
		
		private var trigger2Tooltip:flash.utils.Dictionary;
		
		private var textField:flash.text.TextField;
		
		private var _root:flash.display.DisplayObjectContainer;
		
		private var container:flash.display.Sprite;
		
		private var _defaultRender:*=null;
		
		private var timer:flash.utils.Timer;
		
		private var delayTimer:flash.utils.Timer;
		
		private var delayTarget:flash.display.InteractiveObject;
	}
}


